package o;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import androidx.annotation.NonNull;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.appgallery.serverreqkit.api.listener.IServerCallBack;
import com.huawei.appmarket.service.apppermission.bean.DownloadPermissionResponse;
import com.huawei.appmarket.service.apppermission.bean.DownloadPermissionResponse.AppPermissionInfo;
import com.huawei.appmarket.service.apppermission.bean.GroupPermissionInfo;
import com.huawei.appmarket.service.apppermission.bean.PermissionInfo;
import com.huawei.appmarket.service.apppermission.bean.SingleAppPermissionInfo;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import o.ewm.n;

public class dts {
    private View ˊ;
    private List<b> ˋ;
    private e ˎ;
    private Handler ˏ;
    private ProgressDialog ॱ;

    public interface e {
        void ˎ(Context context, int i);

        void ॱ(Context context);
    }

    public static class b {
        private String ˊ;
        private int ˋ;
        private String ˏ;

        public int ˎ() {
            return this.ˋ;
        }

        public void ˊ(int i) {
            this.ˋ = i;
        }

        public String ˏ() {
            return this.ˏ;
        }

        public void ˊ(String str) {
            this.ˏ = str;
        }

        public String ˋ() {
            return this.ˊ;
        }

        public void ˏ(String str) {
            this.ˊ = str;
        }
    }

    public dts(int i, String str, String str2, e eVar) {
        this.ˏ = new Handler(this, Looper.getMainLooper()) {
            final /* synthetic */ dts ˏ;

            public void handleMessage(Message message) {
                if (message.what == 1 && this.ˏ.ॱ != null && !this.ˏ.ॱ.isShowing() && !ActivityUtil.isAcitivtyNotValid(this.ˏ.ॱ.getContext())) {
                    try {
                        this.ˏ.ॱ.show();
                        ayw.ˊ(this.ˏ.ॱ.getWindow());
                    } catch (Exception e) {
                        Logger.e("AppPermissionManager", "handleMessage, ex = " + e.toString());
                    }
                }
            }
        };
        this.ˋ = new ArrayList();
        b bVar = new b();
        bVar.ˋ = i;
        bVar.ˏ = str2;
        bVar.ˊ = str;
        this.ˋ.add(bVar);
        this.ˎ = eVar;
    }

    public dts(@NonNull List<b> list, e eVar) {
        this.ˏ = /* anonymous class already generated */;
        this.ˋ = list;
        this.ˎ = eVar;
    }

    public void ˊ(View view) {
        this.ˊ = view;
    }

    private void ˏ() {
        Iterator it = this.ˋ.iterator();
        while (it.hasNext()) {
            b bVar = (b) it.next();
            if (TextUtils.isEmpty(bVar.ˋ())) {
                Logger.e("AppPermissionManager", "the app's packageName is null, can't show app permissions");
                it.remove();
            } else if (((cdg) cdb.ॱ(cdg.class)).ॱॱ(bVar.ˋ())) {
                Logger.ˏ("AppPermissionManager", "app is update,do not show permission");
                it.remove();
            } else if (VERSION.SDK_INT >= 23 && bVar.ˎ() >= 23) {
                Logger.ˏ("AppPermissionManager", "app targetSdkVersion bigger than 23:" + bVar.ˋ());
                it.remove();
            }
        }
    }

    private boolean ॱ() {
        ˏ();
        if (this.ˋ.isEmpty()) {
            return false;
        }
        Logger.ˏ("AppPermissionManager", "show AppPermission by Policy:" + this.ˋ.size());
        return true;
    }

    private void ˊ(final Context context) {
        StringBuilder stringBuilder = new StringBuilder(DnsConfig.MAX_CACHE_ENTRIES);
        for (int i = 0; i < this.ˋ.size() - 1; i++) {
            stringBuilder.append(((b) this.ˋ.get(i)).ˋ()).append(',');
        }
        stringBuilder.append(((b) this.ˋ.get(this.ˋ.size() - 1)).ˋ());
        dtl o_dtl = new dtl(stringBuilder.toString());
        this.ॱ = new ProgressDialog(context);
        this.ॱ.setCancelable(true);
        this.ॱ.setMessage(context.getString(n.ˊߴ));
        this.ˏ.sendMessageDelayed(this.ˏ.obtainMessage(1), 500);
        if (context instanceof Activity) {
            o_dtl.setServiceType_(dgm.ˎ((Activity) context));
        }
        final AsyLoader ˎ = ˎ(context, o_dtl);
        this.ॱ.setOnCancelListener(new OnCancelListener(this) {
            final /* synthetic */ dts ˋ;

            public void onCancel(DialogInterface dialogInterface) {
                Logger.ˎ("AppPermissionManager", "user cancel call store permission.");
                if (ˎ != null) {
                    ˎ.ˎ(true);
                }
                this.ˋ.ˎ();
                this.ˋ.ˎ.ॱ(context);
            }
        });
    }

    private AsyLoader ˎ(final Context context, dtl o_dtl) {
        return cfw.ˎ(o_dtl, new IServerCallBack(this) {
            final /* synthetic */ dts ˎ;

            public void prePostResult(BaseRequest o_cvu, ResponseBean responseBean) {
            }

            public void notifyResult(BaseRequest o_cvu, ResponseBean responseBean) {
                this.ˎ.ˏ.removeMessages(1);
                if (this.ˎ.ॱ != null && this.ˎ.ॱ.isShowing()) {
                    try {
                        this.ˎ.ॱ.dismiss();
                        this.ˎ.ॱ = null;
                    } catch (IllegalArgumentException e) {
                        Logger.e("AppPermissionManager", "Loading Dialog IllegalArgumentException");
                    }
                }
                if (responseBean.getResponseCode() == 0 && responseBean.getRtnCode_() == 0) {
                    DownloadPermissionResponse downloadPermissionResponse = (DownloadPermissionResponse) responseBean;
                    if (downloadPermissionResponse.ॱ() == null || downloadPermissionResponse.ॱ().isEmpty()) {
                        Logger.ˎ("AppPermissionManager", "Permission is null or empty");
                        this.ˎ.ˎ();
                        this.ˎ.ˎ.ˎ(context, 0);
                        return;
                    }
                    ArrayList ˊ = dts.ˊ(downloadPermissionResponse.ॱ(), this.ˎ.ˋ);
                    if (ˊ.isEmpty()) {
                        Logger.ˎ("AppPermissionManager", "Permission is empty after sorted");
                        this.ˎ.ˎ.ˎ(context, 0);
                        this.ˎ.ˎ();
                        return;
                    }
                    this.ˎ.ˎ(ˊ, context);
                    return;
                }
                Logger.ˎ("AppPermissionManager", "call store failed:" + responseBean.getRtnCode_());
                this.ˎ.ˎ();
                this.ˎ.ˎ.ˎ(context, 0);
            }
        });
    }

    private void ˎ(final ArrayList<SingleAppPermissionInfo> arrayList, Context context) {
        dtt ˋ = dtt.ˋ(context, arrayList);
        ˋ.ˋ(new ewa(this) {
            final /* synthetic */ dts ˏ;

            public void ˊ(final Activity activity, DialogInterface dialogInterface) {
                Logger.ˎ("AppPermissionManager", "cancel the PermissionDialog");
                new Handler(Looper.getMainLooper()).post(new Runnable(this) {
                    final /* synthetic */ AnonymousClass5 ˏ;

                    public void run() {
                        this.ˏ.ˏ.ˎ.ॱ(activity);
                        this.ˏ.ˏ.ˎ();
                    }
                });
            }

            public void ˏ(final Activity activity, DialogInterface dialogInterface, int i) {
                if (i == -1) {
                    Logger.ˎ("AppPermissionManager", "agree the PermissionDialog");
                    new Handler(Looper.getMainLooper()).post(new Runnable(this) {
                        final /* synthetic */ AnonymousClass5 ˋ;

                        public void run() {
                            this.ˋ.ˏ.ˎ.ˎ(activity, arrayList.size());
                            this.ˋ.ˏ.ˎ();
                        }
                    });
                } else if (i == -2) {
                    Logger.ˎ("AppPermissionManager", "disagree the PermissionDialog");
                    new Handler(Looper.getMainLooper()).post(new Runnable(this) {
                        final /* synthetic */ AnonymousClass5 ˎ;

                        public void run() {
                            this.ˎ.ˏ.ˎ.ॱ(activity);
                            this.ˎ.ˏ.ˎ();
                        }
                    });
                }
            }
        });
        ˋ.ॱ(-1, context.getString(n.ˑʽ));
        ˋ.ˋ(context, "AppPermissionManager" + hashCode());
    }

    private void ˎ() {
        if (this.ˊ != null) {
            this.ˊ.setClickable(true);
        }
    }

    public static ArrayList<SingleAppPermissionInfo> ˊ(List<AppPermissionInfo> list, List<b> list2) {
        ArrayList<SingleAppPermissionInfo> arrayList = new ArrayList();
        for (AppPermissionInfo appPermissionInfo : list) {
            if (appPermissionInfo.ˏ() < 23 || VERSION.SDK_INT < 23) {
                ˏ(appPermissionInfo, ॱ(appPermissionInfo, ॱ(arrayList, appPermissionInfo, list2)));
            } else {
                Logger.ˎ("AppPermissionManager", "App targetSdkVersion bigger than 23, and Device is bigger than 23");
            }
        }
        return arrayList;
    }

    private static void ˏ(AppPermissionInfo appPermissionInfo, GroupPermissionInfo groupPermissionInfo) {
        List ˏ = groupPermissionInfo.ˏ();
        if (ˏ == null) {
            ˏ = new ArrayList();
            groupPermissionInfo.ˎ(ˏ);
        }
        ˏ.add(new PermissionInfo(appPermissionInfo.ॱ(), appPermissionInfo.ˋ()));
    }

    @NonNull
    private static GroupPermissionInfo ॱ(AppPermissionInfo appPermissionInfo, SingleAppPermissionInfo singleAppPermissionInfo) {
        List ॱ = singleAppPermissionInfo.ॱ();
        List list;
        if (ॱ == null) {
            ॱ = new ArrayList();
            singleAppPermissionInfo.ˊ(ॱ);
            list = ॱ;
        } else {
            list = ॱ;
        }
        for (GroupPermissionInfo groupPermissionInfo : r1) {
            if (groupPermissionInfo.ˎ() != null && groupPermissionInfo.ˎ().equals(appPermissionInfo.ˋ())) {
                break;
            }
        }
        GroupPermissionInfo groupPermissionInfo2 = null;
        if (groupPermissionInfo2 != null) {
            return groupPermissionInfo2;
        }
        groupPermissionInfo2 = new GroupPermissionInfo(appPermissionInfo.ˋ());
        r1.add(groupPermissionInfo2);
        return groupPermissionInfo2;
    }

    @NonNull
    private static SingleAppPermissionInfo ॱ(ArrayList<SingleAppPermissionInfo> arrayList, AppPermissionInfo appPermissionInfo, List<b> list) {
        SingleAppPermissionInfo singleAppPermissionInfo;
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            singleAppPermissionInfo = (SingleAppPermissionInfo) it.next();
            if (singleAppPermissionInfo.ˊ().equals(appPermissionInfo.ˊ())) {
                break;
            }
        }
        singleAppPermissionInfo = null;
        if (singleAppPermissionInfo != null) {
            return singleAppPermissionInfo;
        }
        String ˏ;
        String str = "";
        for (b bVar : list) {
            if (bVar.ˋ().equals(appPermissionInfo.ˊ())) {
                ˏ = bVar.ˏ();
                break;
            }
        }
        ˏ = str;
        SingleAppPermissionInfo singleAppPermissionInfo2 = new SingleAppPermissionInfo(appPermissionInfo.ˊ(), ˏ);
        arrayList.add(singleAppPermissionInfo2);
        return singleAppPermissionInfo2;
    }

    public void ˋ(final Context context) {
        if (Looper.getMainLooper().getThread().getId() == Thread.currentThread().getId()) {
            ˎ(context);
        } else {
            new Handler(Looper.getMainLooper()).post(new Runnable(this) {
                final /* synthetic */ dts ˋ;

                public void run() {
                    this.ˋ.ˎ(context);
                }
            });
        }
    }

    private void ˎ(Context context) {
        if (this.ˊ == null || this.ˊ.isClickable()) {
            if (this.ˊ != null) {
                this.ˊ.setClickable(false);
            }
            if (ॱ()) {
                ˊ(context);
                return;
            }
            this.ˎ.ˎ(context, 0);
            ˎ();
            return;
        }
        Logger.ˎ("AppPermissionManager", "click too frequently");
    }
}
